from rdflib import Graph, URIRef, Literal, Namespace
from rdflib.namespace import RDF, RDFS, OWL

# 创建一个新的图
g = Graph()

# 定义命名空间
MACRO = Namespace("http://example.org/macro/")
POWER = Namespace("http://example.org/power/")
SCHEMA = Namespace("http://schema.org/")
TIME = Namespace("http://www.w3.org/2006/time#")

# 定义宏观经济实体
gdp = URIRef(MACRO["GDP"])
unemployment_rate = URIRef(MACRO["UnemploymentRate"])
inflation_rate = URIRef(MACRO["InflationRate"])
interest_rate = URIRef(MACRO["InterestRate"])

# 定义电力实体
power_consumption = URIRef(POWER["PowerConsumption"])
power_generation = URIRef(POWER["PowerGeneration"])
renewable_power = URIRef(POWER["RenewablePower"])
non_renewable_power = URIRef(POWER["NonRenewablePower"])

# 定义时间实体
time_2023 = URIRef(TIME["2023"])
time_2024 = URIRef(TIME["2024"])

# 添加实体类型和标签
# 宏观经济指标
g.add((gdp, RDF.type, SCHEMA["EconomicIndicator"]))
g.add((gdp, RDFS.label, Literal("国内生产总值")))
g.add((unemployment_rate, RDF.type, SCHEMA["EconomicIndicator"]))
g.add((unemployment_rate, RDFS.label, Literal("失业率")))
g.add((inflation_rate, RDF.type, SCHEMA["EconomicIndicator"]))
g.add((inflation_rate, RDFS.label, Literal("通货膨胀率")))
g.add((interest_rate, RDF.type, SCHEMA["EconomicIndicator"]))
g.add((interest_rate, RDFS.label, Literal("利率")))

# 电力指标
g.add((power_consumption, RDF.type, SCHEMA["Measurement"]))
g.add((power_consumption, RDFS.label, Literal("电力消费量")))
g.add((power_generation, RDF.type, SCHEMA["Measurement"]))
g.add((power_generation, RDFS.label, Literal("电力生产量")))
g.add((renewable_power, RDF.type, SCHEMA["Measurement"]))
g.add((renewable_power, RDFS.label, Literal("可再生能源发电量")))
g.add((non_renewable_power, RDF.type, SCHEMA["Measurement"]))
g.add((non_renewable_power, RDFS.label, Literal("非可再生能源发电量")))

# 时间
g.add((time_2023, RDF.type, TIME["Instant"]))
g.add((time_2023, RDFS.label, Literal("2023年")))
g.add((time_2024, RDF.type, TIME["Instant"]))
g.add((time_2024, RDFS.label, Literal("2024年")))

# 添加属性值
g.add((gdp, SCHEMA["value"], Literal(100000, datatype="http://www.w3.org/2001/XMLSchema#decimal")))
g.add((gdp, SCHEMA["inTimePeriod"], time_2023))
g.add((unemployment_rate, SCHEMA["value"], Literal(5.0, datatype="http://www.w3.org/2001/XMLSchema#float")))
g.add((unemployment_rate, SCHEMA["inTimePeriod"], time_2023))
g.add((power_consumption, SCHEMA["value"], Literal(5000, datatype="http://www.w3.org/2001/XMLSchema#decimal")))
g.add((power_consumption, SCHEMA["inTimePeriod"], time_2023))
g.add((power_generation, SCHEMA["value"], Literal(5500, datatype="http://www.w3.org/2001/XMLSchema#decimal")))
g.add((power_generation, SCHEMA["inTimePeriod"], time_2023))

# 添加关系
# 电力生产与消费关系
g.add((power_generation, SCHEMA["contributesTo"], power_consumption))
# 可再生与非可再生电力和总电力生产的关系
g.add((renewable_power, SCHEMA["partOf"], power_generation))
g.add((non_renewable_power, SCHEMA["partOf"], power_generation))
# 宏观经济与电力数据的关系
g.add((gdp, SCHEMA["influences"], power_consumption))
g.add((interest_rate, SCHEMA["influences"], power_generation))
# 时间关系
g.add((gdp, SCHEMA["inTimePeriod"], time_2023))
g.add((power_consumption, SCHEMA["inTimePeriod"], time_2023))

# 添加等价类和子类关系（示例）
g.add((renewable_power, RDFS.subClassOf, power_generation))
g.add((non_renewable_power, RDFS.subClassOf, power_generation))

# 保存知识图谱
g.serialize(destination='complex_macro_power_kg.ttl', format='turtle')


import rdflib
import networkx as nx
import matplotlib.pyplot as plt

# 读取 .ttl 文件
g = rdflib.Graph()
g.parse('complex_macro_power_kg.ttl', format='turtle')

# 创建一个空的有向图
G = nx.DiGraph()

# 遍历图中的三元组
for s, p, o in g:
    # 添加节点
    G.add_node(s)
    G.add_node(o)
    # 添加边
    G.add_edge(s, o, label=p)

# 绘制图形
pos = nx.spring_layout(G)
nx.draw(G, pos, with_labels=True, node_color='lightblue', node_size=1500, font_size=8)
edge_labels = nx.get_edge_attributes(G, 'label')
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, font_size=6)

# 显示图形
plt.title('Knowledge Graph Visualization')
plt.show()